package cn.xinfei.xdecision.service;

import cn.xinfei.xdecision.common.model.configcenter.rejectcode.model.RejectBranchCodeModel;
import cn.xinfei.xdecision.common.model.enginex.po.Engine;
import cn.xinfei.xdecision.common.model.enginex.po.EngineVersion;
import cn.xinfei.xdecision.common.model.enginex.vo.EngineVersionInOutVarsRespVo;
import cn.xinfei.xdecision.common.vo.FieldInfoVo;
import cn.xinfei.xdecision.engine.runner.executor.NodeHandler;
import cn.xinfei.xdecision.engine.runner.metadata.MetadataProvider;
import cn.xinfei.xdecision.model.CacheManagerRequest;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Table;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Slf4j
@Service
public class MetadataCacheService {

    @Autowired
    private MetadataProvider metadataProvider;

    public void reload(CacheManagerRequest cacheManagerRequest) {

        String type = cacheManagerRequest.getType();
        if (StringUtils.isNotEmpty(type)) {
            if (CacheManagerRequest.CacheTypeEnum.REJECT.name().equalsIgnoreCase(type)) {
                Map<Long, RejectBranchCodeModel> data = metadataProvider.getRejectCodeLoader().init(cacheManagerRequest.getId());
                if (data != null && data.size() > 0) {
                    MetadataProvider.rejectBranchCodeMapCache.putAll(data);
                }
            } else if (CacheManagerRequest.CacheTypeEnum.FIELD.name().equalsIgnoreCase(type)) {
                Map<String, FieldInfoVo> data = metadataProvider.getFieldInfoLoader().init(cacheManagerRequest.getCode());
                if (data != null && data.size() > 0) {
                    MetadataProvider.fieldMapCache.putAll(data);
                }
            } else if (CacheManagerRequest.CacheTypeEnum.ENGINE.name().equalsIgnoreCase(type)) {
                Table<String, Long, Engine> engineTable = metadataProvider.getEngineInfoLoader().init(cacheManagerRequest.getCode());
                Map<String, EngineVersion> engineVersionMap = metadataProvider.getDecisionFlowLoader().init(engineTable);
                Table<String, String, NodeHandler> nodeHandlerTable = metadataProvider.getDecisionFlowLoader().init(engineVersionMap);
                log.info("nodeHandlerTable=====>{}", nodeHandlerTable.size());
                Table<String, Long, EngineVersionInOutVarsRespVo> inOutVarsRespVoTable = metadataProvider.getEngineInOutFieldLoader().init(engineTable);
                if (engineTable != null && engineTable.size() > 0) {
                    MetadataProvider.engineAndVersionMapCache.putAll(engineTable);
                }
                if (engineVersionMap != null && engineVersionMap.size() > 0) {
                    MetadataProvider.engineVersionMapCache.putAll(engineVersionMap);
                }
                if (nodeHandlerTable != null && nodeHandlerTable.size() > 0) {
                    MetadataProvider.engineAndNodeMapCache.putAll(nodeHandlerTable);
                    log.info("MetadataProvider.engineAndNodeMapCache=====>{}", MetadataProvider.engineAndNodeMapCache.size());
                }
                if (inOutVarsRespVoTable != null && inOutVarsRespVoTable.size() > 0) {
                    MetadataProvider.inOutVarsCache.putAll(inOutVarsRespVoTable);
                }
            } else {

                log.info("不支持的方式...cacheManagerRequest={}", JSONObject.toJSONString(cacheManagerRequest));
            }
        } else {
            log.info("全量缓存加载...");
            metadataProvider.init();
        }

    }
}
